package com.tchy.cloud.controller;

import com.tchy.cloud.entity.ABankEntity;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 银行卡信息维护
 */
@Controller
@RequestMapping("/bankController")
public class ABankController {
    private static final Logger logger = Logger.getLogger(ABankEntity.class.getName());

    @Autowired
    private CommonService commonService;

    /**
     * 银行卡信息列表页面跳转
     *
     * @return
     */
    @RequestMapping(params = "indexView")
    public ModelAndView indexView() {
        return new ModelAndView("clound/bank/indexView");
    }

    /**
     * 银行卡信息列表数据展示
     *
     * @param aBankEntity
     * @param request
     * @param response
     * @param dataGrid
     */
    @RequestMapping(params = "datagrid")
    public void datagrid(ABankEntity aBankEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(ABankEntity.class, dataGrid);
        cq.addOrder("id", SortDirection.desc);
        cq.add();
        // 查询条件组装
        HqlGenerateUtil.installHql(cq, aBankEntity, request.getParameterMap());
        commonService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 新增或编辑页面跳转
     *
     * @param aBankEntity
     * @param req
     * @return
     */
    @RequestMapping(params = "addorupdateView")
    public ModelAndView addorupdateView(ABankEntity aBankEntity, HttpServletRequest req) {
        if (aBankEntity.getId() != null) {
            aBankEntity = commonService.getEntity(ABankEntity.class, aBankEntity.getId());
        }
        req.setAttribute("bank", aBankEntity);
        return new ModelAndView("clound/bank/addorupdateView");
    }


    /**
     * 新增及修改的数据处理
     * @param aBankEntity
     * @return
     */
    @RequestMapping(params = "saveorupdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson saveorupdate(ABankEntity aBankEntity) {
        AjaxJson j = new AjaxJson();
        try {
            if (aBankEntity.getId() != null) {//修改
                ABankEntity entity = commonService.get(ABankEntity.class, aBankEntity.getId());
                MyBeanUtils.copyBeanNotNull2Bean(aBankEntity, entity);
                commonService.saveOrUpdate(entity);
            } else {//保存
                commonService.save(aBankEntity);
            }
            j.setMsg("操作成功");
        } catch (Exception e) {
            logger.error(e);
            j.setMsg("操作失败");
        }
        return j;
    }

}







